package org.yangspring.samples.mvc.simple;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * Created by franklin on 6/4/2017.
 */
public class test111 {
}

abstract class SysListener extends HttpServlet implements ServletContextListener {

    private String ROOTPATH;
    private Boolean USE_URL_REWRITE;

    private static final Log logger = LogFactory.getLog(SysListener.class);

    public void contextDestroyed(ServletContextEvent sce) {   //用于在容器关闭时,操作

        //用于在容器开启时,操作
        // public void contextInitialized(ServletContextEvent sce) {
        String rootpath = sce.getServletContext().getRealPath("/");
        logger.warn("-------------rootPath:" + rootpath);

        if (rootpath != null) {
            rootpath = rootpath.replaceAll("\\\\", "/");
        } else {
            rootpath = "/";
        }
        if (!rootpath.endsWith("/")) {
            rootpath = rootpath + "/";
        }

        this.ROOTPATH = rootpath;
        logger.info("Application Run Path:" + rootpath);
        String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");
        boolean burlrewrtie = false;

        if (urlrewrtie != null) {
            burlrewrtie = Boolean.parseBoolean(urlrewrtie);
        }

        this.USE_URL_REWRITE = burlrewrtie;

        logger.info("Use Urlrewrite:" + burlrewrtie);
        //    其它略之....
    }
}

/*最终输出
-------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Urlrewrite:true
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Cluster:false
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]SERVLET MAPPING:*.bbscs
2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Post Storage Mode:1
*/

class MainServlet extends HttpServlet {

    private static final Log logger = LogFactory.getLog(MainServlet.class);

    public MainServlet() {
        super();
    }

    public void init() throws ServletException {
        logger.warn("下面的两个参数param1是在servlet中存放的");
        logger.warn(this.getInitParameter("param1"));
        logger.warn("下面的参数是存放在servletcontext中的");
        logger.warn(getServletContext().getInitParameter("context/param"));
    }
}

/*
* 第一种参数：application范围内的参数，存放在servletcontext
* 第二种参数：servlet范围内的参数
*
*
* 第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
* 第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得
*
* */